<?php

// phpMyRealty 3
//
// File Name: sitemap.php
// File Location : ./
//
// Copyright (c)2009 phpMyRealty.com
//
// e-mail: support@phpMyRealty.com

// Include configuration file and general functions
define('PMR', 'true');

$page = 'sitemap';

include ( './config.php' );
include ( PATH . '/defaults.php' );

// ----------------------------------------------------------------------
// INDEX PAGE SECTION

// Title tag content
$title = $conf['website_name_short'];

// Template header
include ( PATH . '/templates/' . $cookie_template . '/header.php' );


// Set the number of listing per row in the table
$setup_rows = 1;

// Select all property types from the database

$sql = 'SELECT id, ' . $language_in . ', counter FROM ' . TYPES_TABLE . ' ORDER BY ' . $language_in;
$r = $db->query ( $sql ) or error ('Critical Error', mysql_error () );;

echo '<table width="100%" cellpadding="5" cellspacing="0" border="0">
       <tr>
        <td width="' . 100 / $setup_rows . '%" align="left" valign="top">
         <ul>
     ';

// Calculate the number of listings per row ($setup_rows rows)
$results_amount = ceil ($db->numrows($r) / $setup_rows);

// Total Results
$results_total = $db->numrows($r);

$results = 0;
$rows = 0;

while ($f = $db->fetcharray($r))

 {

  $rows++;
  $results++;

  if ($conf['rewrite'] == 'ON')
   echo '<li> <a href="' . URL . '/Type/' . rewrite ( $f[1] ) . '/' . $f['id'] . '.html">' . $f[1] . '</a><br />';
  else
   echo '<li> <a href="' . URL . '/search.php?type=' . $f['id'] . '">' . $f[1] . '</a><br />';

	// Title/descr language to use (if available)
	$title = str_replace( 'name', 'title', $language_in );
	$description = str_replace( 'name', 'description', $language_in );

  $sql1 = 'SELECT ' . $title . ', ' . $description . ', ' . PROPERTIES_TABLE . '.* FROM ' . PROPERTIES_TABLE  . ' WHERE type = ' . $f['id'] . ' ORDER BY title';
  $sqlr = $db->query($sql1);

  echo '<ul>';

  while ($sqlf = $db->fetcharray($sqlr))
   {
   
	// Default
	if ($sqlf[0] == '')
		$sqlf['title'] = $sqlf['title'];
	else
		$sqlf['title'] = $sqlf[0];
		
	if ($sqlf[1] == '')
		$sqlf['description'] = $sqlf['description'];
	else
		$sqlf['description'] = $sqlf[1];

    $sql2 = 'SELECT * FROM ' . USERS_TABLE . ' WHERE id = ' . $sqlf['userid'];
    $sql2r = $db->query($sql2);
    $sql2f = $db->fetcharray($sql2r);

    if ($conf['rewrite'] == 'ON')
     echo '<li><a href="' . URL . '/Listing/' . rewrite ( getnamebyid ( TYPES_TABLE, $sqlf['type'] ) ) . '/' . $sqlf['id'] . '_' . rewrite($sqlf['title']) . '.html">' . $sqlf['title'] . '</a> ( by <a href="' . URL . '/Realtor/' . $sql2f['id'] . '.html">' . $sql2f['first_name'] . ' ' . $sql2f['last_name'] . '</a>)</li>';
    else
     echo '<li><a href="' . URL . '/viewlisting.php?id=' . $sqlf['id'] . '">' . $sqlf['title'] . '</a> ( by <a href="' . URL . '/viewuser.php?id=' . $sql2f['id'] . '">' . $sql2f['first_name'] . ' ' . $sql2f['last_name'] . '</a>)</li>';

   }

  echo '</ul>';

  // Restart the row each time we reach the maximum listing per row
  // Do not create new row if this is a last element of the array
  if ($rows == $results_amount && $results != $results_total)

   {
    echo ' </ul> 
            </td>
            <td width="' . 100 / $setup_rows . '%" align="left" valign="top">
           <ul>
         ';
    // Clean the $rows variable for the new row listings counter
    $rows = 0;
   }

 }

echo '   </ul>
	</td>
       </tr>
      </table>
     ';


// Template footer
include ( PATH . '/templates/' . $cookie_template . '/footer.php' );

?>